package com.example.dessert.controller.fore;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.dessert.annotation.LoginCheck;
import com.example.dessert.controller.BaseController;
import com.example.dessert.entity.Favorite;
import com.example.dessert.entity.Product;
import com.example.dessert.entity.ProductCategory;
import com.example.dessert.entity.ProductSpec;
import com.example.dessert.service.FavoriteService;
import com.example.dessert.service.ProductCategoryService;
import com.example.dessert.service.ProductService;
import com.example.dessert.service.ProductSpecService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @ClassName: ProductController
 * @Description:
 * @Author li
 * @Date 2022/1/11
 * @Version 1.0
 */
@Controller
@RequestMapping("/fore/product")
public class ProductController extends BaseController  {


    @Resource
    private ProductService productService;
    @Resource
    private ProductCategoryService productCategoryService;
    @Resource
    private ProductSpecService productSpecService;
    @Resource
    private FavoriteService favoriteService;

    /**
     * 商品列表
     */
    @GetMapping("/list")
    @LoginCheck
    public String list(Model model, @RequestParam(name = "category") Long category, @RequestParam(name = "page", defaultValue = "1") Integer page ) {
        logger.info("category ID:{}", category);
        logger.info("current page number:{}", page);

        ProductCategory pCategory=productCategoryService.queryProductCategoryBuyId(category);

        IPage<Product> product= productService.queryProductListBuyCategory(category, page);
        logger.info("Query product results :{}", product);

        model.addAttribute("product", product);
        model.addAttribute("category", category);
        model.addAttribute("page", page);
        model.addAttribute("pCategory", pCategory);

        return "fore/product/list";

    }

    /**
     * 商品详细
     */
    @GetMapping("/detail")
    @LoginCheck
    public String detail(HttpSession session, Model model, @RequestParam(name = "productId") Long productId) {
        logger.info("product ID:{}", productId);

        Product product= productService.queryProductBuyId(productId);
        logger.info("Query product buy Id results :{}", product);

        List<ProductSpec> specs = productSpecService.queryProductSpecList(productId);
        logger.info("Query product spec buy productId results :{}", product);

        Object userId = session.getAttribute("userId");
        Favorite userFavorite= favoriteService.queryFavoriteBuyUserId((Long) userId,productId);
        logger.info("Query whether the user has favorite the product result:{}", userFavorite);

        model.addAttribute("product", product);
        model.addAttribute("specs", specs);
        model.addAttribute("userFavorite", userFavorite);

        return "fore/product/detail";

    }


}
